package com.gluck.cases.boostrap.controller;

import com.github.pagehelper.PageInfo;
import com.gluck.cases.app.biz.impl.AdminUserService;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.annotations.JwtFilterSetControl;
import com.gluck.cases.common.utils.exception.BizSerException;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.BizResult;
import com.gluck.cases.core.modal.entity.AdminUser;
import com.gluck.cases.core.modal.req.AdminUserDeleteReq;
import com.gluck.cases.core.modal.req.AdminUserReq;
import com.gluck.cases.core.modal.req.AdminUserSearchReq;
import com.gluck.cases.core.modal.req.PageReq;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @ClassName UserController
 * @Desc 用户中心
 * @Author I am Gluck
 * @Date 2022/5/19 - 11:33 上午
 **/
@RestController
@RequestMapping(value = "/user",name = "用户中心")
public class UserController {

    @Autowired
    private AdminUserService adminUserService;

    /**
     * 功能描述: 登陆
     * @Params [userReq, response]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, name = "登陆")
    @JwtFilterSetControl
    public BizResult login(@RequestBody AdminUserReq userReq, HttpServletResponse response){
        if(StringUtils.isEmpty(userReq.getPassword()) || StringUtils.isEmpty(userReq.getUsername())){
            AssertUtil.isTrue(true, BizSerErrorEnum.REQUEST_PARAMS_ERROR);
        }
        boolean queryUser = adminUserService.queryUser(userReq);
        if(!queryUser){
            AssertUtil.isTrue(true, BizSerErrorEnum.SELECT_ERROR);
        }
        String token = adminUserService.buildUserToken(userReq);
        response.setHeader("Authorization", token);
        response.setHeader("Access-Control-Expose-Headers", "Authorization");
        return BizResult.newSuccessResult(true);
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET, name = "logout")
    public BizResult logout(@RequestParam(name = "userId") Integer i,@RequestParam(name = "user") String s,@RequestParam(name = "test") Long l){
        System.out.println("logout");
        throw new BizSerException(BizSerErrorEnum.AUTH_LIST_ERROR);
    }

    /**
     * 功能描述: 创建用户
     * @Params [userReq]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST, name = "创建用户")
    public BizResult create(@RequestBody AdminUserReq userReq){
        boolean result = adminUserService.createNewUser(userReq);
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 用户列表
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET, name = "用户列表")
    public BizResult list(@RequestBody PageReq req){
        if(Objects.isNull(req.getPage()) || Objects.isNull(req.getSize())){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        PageInfo<AdminUser> result = adminUserService.queryUserList(req.getPage(), req.getSize());
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 更新密码
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/change_pass", method = RequestMethod.POST, name = "更新密码")
    public BizResult changePass(@RequestBody AdminUserReq req){
        if(Objects.isNull(req.getUsername()) || Objects.isNull(req.getPassword())){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        boolean result = adminUserService.changePassword(req.getUsername(), req.getPassword());
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 更新用户信息
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, name = "更新用户信息")
    public BizResult updateAdminUser(@RequestBody AdminUserReq req){
        if(Objects.isNull(req.getUsername()) || Objects.isNull(req.getIsLocked())){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        AdminUser adminUser = adminUserService.updateUser(req);
        return BizResult.newSuccessResult(adminUser);
    }

    /**
     * 功能描述: 单个用户删除
     * @Params [userId]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET, name = "单个用户删除")
    public BizResult delete(@RequestParam(name = "userId") Long userId){
        if(userId == 0 || Objects.isNull(userId)){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        boolean result = adminUserService.deleteUser(userId);
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 批量删除用户
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/delete_list", method = RequestMethod.GET, name = "批量删除用户")
    public BizResult deleteList(@RequestBody AdminUserDeleteReq req){
        if(req.getIds().size() == 0){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        boolean result = adminUserService.deleteUsers(req.getIds());
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 用户列表搜索
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/user_search", method = RequestMethod.GET, name = "用户列表搜索")
    public BizResult queryRoleAndUser(@RequestBody AdminUserSearchReq req){
        if(Objects.isNull(req.getPage()) || Objects.isNull(req.getSize())){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        PageInfo<AdminUser> adminUserPageInfo = adminUserService.searchUsers(req);
        return BizResult.newSuccessResult(adminUserPageInfo);
    }

}
